<template>
    <!-- 列表式 商品展示 -->
    <div class="an-goods-list">
        <van-image class="an-img" width="100" height="100" :src="goods.image" @click="toUrl"/>
        <div class="an-text">
            <div class="van-multi-ellipsis--l2 an-title" @click="toUrl">{{goods.name}}</div>
            <div class="an-subtitle" v-if="isOption">{{goodsActive.name}}</div>
            <div class="an-hang">
                <div class="an-money" >
                  ￥
                  <strong v-if="isOption">{{goodsActive.price}}</strong>
                  <strong v-else>{{goods.price}}</strong>
                </div>
                <div class="an-plus" v-if="stock" @click="addCart">
                    <van-icon name="shopping-cart" />
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
  props:{
    goods:{
      type: Object,
      default:() => {}
    }
  },
  data() {
    return{
      stock:0, // 库存
      goodsActive:{},
      isOption: false // 是否存在多规格选择
    };
  },
  created() {
    this.anLoad();
  },
  methods:{

    anLoad() {
      if(this.goods.options) {
        try{
          let nd = JSON.parse(this.goods.options);
          // 默认展示第一个规格
          this.goodsActive = nd[0];
          this.stock = this.goodsActive.stock;
          this.isOption = true;
        }catch(e) {console.log(e);}
      }else{
        this.stock = this.goods.stock;
      }
    },

    // 加入购物车
    addCart() {
      this.$auth.addCart(this.goods, this.goodsActive);
    },

    toUrl() {
      this.$router.push({ path: '/goods', query: { id: this.goods.id }});
    }
  }
};
</script>

<style lang="scss" scoped>
@import '../../css/mixin.scss';
.an-goods-list{
    display: flex;
    font-size: 14px;
    .an-img{
        margin-right: 5px;
        flex:0 0 auto;
    }
    .an-text{
        flex:1 1 auto;
        position: relative;
        .an-title{
          font-size: 16px;
        }
        .an-subtitle{
          color: $gray-6;
          padding: 5px 0;
        }
        .an-hang{
            position: absolute;
            bottom: 5px;
            left: 0;
            right: 0;
            .an-money{
                color: $orange-dark;
                strong{
                    font-size: 18px;
                }
            }
            .an-plus{
                position:absolute;
                right: 0;
                bottom: 0;
                width: 30px;
                height: 30px;
                border-radius: 50%;
                display: flex;
                justify-content: center;
                align-items: center;
                color: #ffffff;
                font-size: 18px;
                background-color: $orange-dark;
            }
        }
    }
}
</style>